package hkr.vfu.bluecenter.backend;

import java.io.*;
import java.text.*;
import java.util.Date;

/**
 * Created by MathiasHansson on 2014-01-06.
 */
public class LogHandler {


    public void sendToFile(File root, BoxVO boxVO) {
        SensorVO[] sensors = boxVO.getSensors();
        String[] values = new String[sensors.length];

        for (int i = 0; i < sensors.length; i++) {
            values[i] = returnSensorValue(sensors[i]);
        }

        root.mkdirs();
        File file = new File(root, boxVO.getLogName());

        FileOutputStream fop;
        FileInputStream fip;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fip = new FileInputStream(file);
            InputStreamReader inw = new InputStreamReader(fip);
            BufferedReader br = new BufferedReader(inw);

            fop = new FileOutputStream(file, true);
            OutputStreamWriter osw = new OutputStreamWriter(fop);
            BufferedWriter bw = new BufferedWriter(osw);

            String firstLine = br.readLine();
            if (firstLine == null) {
                firstLine = makeFirstRow(boxVO);
                br.close();
                bw.write(firstLine);
                bw.newLine();
            } else {
                firstLine = "" + firstLine;
                String newFirstLine = makeFirstRow(boxVO);
                if (!firstLine.equals(newFirstLine)) {
                    String line;
                    StringBuilder builder = new StringBuilder();
                    builder.append(newFirstLine);
                    while ((line = br.readLine()) != null) {
                        builder.append("\n");
                        builder.append(line);
                    }
                    br.close();
                    firstLine = builder.toString();
                    file.delete();
                    file.createNewFile();

                    fop = new FileOutputStream(file, true);
                    osw = new OutputStreamWriter(fop);
                    bw = new BufferedWriter(osw);

                    bw.write(firstLine);
                    bw.newLine();
                }
            }

            bw.write(makeValueLine(values, sensors[0].getTimeOfSample()));
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String makeFirstRow(BoxVO boxVO) {

        String makeFirstRow = "Date/Time";
        SensorVO[] sensorVOs = boxVO.getSensors();
        for (SensorVO s : sensorVOs) {
            makeFirstRow += "\t" + s.getSensorName() + " " + s.getUnitString();
        }
        return makeFirstRow;
    }

    public String makeValueLine(String[] values, Date date) {

        String makeValueLine;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,S");
        String reportDate = df.format(date);

        String[] milli = reportDate.split(",");
        int millis = Integer.parseInt(milli[1]);
        millis = Math.round(millis / 100);
        reportDate = milli[0] + "," + millis;

        makeValueLine = reportDate;

        for (int i = 0; i < values.length; i++) {

            makeValueLine = makeValueLine + ("\t" + values[i]);
        }

        return makeValueLine;

    }

    public String returnSensorValue(SensorVO sensorVO) {
        double sensorValue = sensorVO.getK() * sensorVO.getValue() + sensorVO.getM();
        String value = "" + sensorValue;
        return value;
    }
}
